var cityId = 0;
var areaId = 0;
var streetId = 0;
var addressStr = '';
var concatName = '';
var concatTel = '';
var citys = [];
var areas = [];
var streets = [];
var addressList = [];
const app = getApp();
Page({
  data:{
    addressList: [] ,
    citys: citys,
    areas: areas,
    streets: streets,
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    areaInfo:"",
    userType:0,
    btn_submit:"填好了 去下单",
    weightItems: [{ code: 1, name: "1", checked: true }, { code: 2, name: "2", checked: false }, { code: 3, name: "3", checked: false }, { code: 4, name: "4", checked: false }, { code: 5, name: "5", checked: false }, { code: 6, name: "6", checked: false }, { code:7 , name: "7-15", checked: false }, { code: 8, name: ">15", checked: false }, { code: 9, name: "不规律", checked: false }]
  },
  onLoad:function(){ 
    var data = this.data;
    wx.request({/**通过code获取openid**/
      url:  getApp().globalData.server_support + "/collect-scrap/queryDistrict",
      method: 'POST',
      success: function (res) {
        console.log("地址请求返回所有信息");
        console.log(res.data);
        console.log(this)
        if (res.data.success) {
          console.log("地址列表信息");
          console.log(res.data.data);
          addressList = res.data.data;;
          citys = addressList;
          areas = addressList[0].dataLists;
          streets = addressList[0].dataLists[0].dataLists;
        } else {
          console.log("获取不到地址信息")
        }
      }
    })
  },
  addressInput:function(e){
    addressStr = e.detail.value
  },
  concatNameInput: function (e) {
    concatName = e.detail.value
  },
  concatPhoneInput: function (e) {
    concatTel = e.detail.value
  }
  ,
  selectDistrict: function (e) {
    var that = this
    console.log("城市地址")
    console.log(citys)
    console.log("区地址")
    console.log(areas)
    console.log("街道地址")
    console.log(streets)
    that.setData({
      addressList: addressList,
      citys: citys,
      areas: areas,
      streets: streets
    })
    // 如果已经显示，不在执行显示动画
    if (that.data.addressMenuIsShow) {
      return
    }
    // 执行显示动画
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow);
    console.log(citys);
    var that = this
    that.setData({
      addressMenuIsShow: isShow
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var data = this.data
    this.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    cityId = data.citys[data.value[0]].districtId;
    areaId = data.areas[data.value[1]].districtId;
    streetId = data.streets[data.value[2]].districtId
    var areaInfo = data.citys[data.value[0]].districtName + "." + data.areas[data.value[1]].districtName
      + "." + data.streets[data.value[2]].districtName
    this.setData({
      areaInfo: areaInfo,
    })
  },
  // 点击蒙版时取消组件的显示
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log(e)
    var value = e.detail.value
    var citys = this.data.citys
    var areas = this.data.areas
    var streets = this.data.streets
    var cityNum = value[0]
    var areaNum = value[1]
    var streetNum = value[2]
    console.log("cityNum=" + cityNum + ":" + value[0] + ":" + this.data.value[0])
    console.log("areaNum=" + areaNum + ":" + value[1] + ":" + this.data.value[1])
    console.log("streetNum=" + streetNum + ":" + value[2] + ":" + this.data.value[2])
    // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
    if (this.data.value[0] != cityNum) {
      var data = this.data;
      this.setData({
        value: [cityNum, 0, 0],
        areas: data.addressList[cityNum].dataLists,
        streets: data.addressList[cityNum].dataLists[0].dataLists
      })
    } else if (this.data.value[1] != areaNum) {
      // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      var data = this.data;
      console.log("选择了区地址")
      console.log(data.addressList[cityNum].dataLists[areaNum])
      this.setData({
        value: [cityNum, areaNum, 0],
        streets: data.addressList[cityNum].dataLists[areaNum].dataLists == null ? []:data.addressList[cityNum].dataLists[areaNum].dataLists,
      })
    } else {
      // 滑动选择了区
      this.setData({
        value: [cityNum, areaNum, streetNum]
      })
    }
    console.log(this.data)
  },
  selAddress:function(){
    console.log("选择地址");
    this.setData({
      addressMenuIsShow:"visible"
    })
  },
  parameterTap: function (e) {//e是获取e.currentTarget.dataset.id所以是必备的，跟前端的data-id获取的方式差不多
    var that = this
    var this_checked = e.currentTarget.dataset.code
    console.log(this_checked)
    var parameterList = this.data.weightItems//获取Json数组
    for (var i = 0; i < parameterList.length; i++) {
      if (parameterList[i].code == this_checked) {
        parameterList[i].checked = true;//当前点击的位置为true即选中
      }
      else {
        parameterList[i].checked = false;//其他的位置为false
      }
    }
    that.setData({
      weightItems: parameterList
    })
  },
  submit: function (e) {
    console.log("开始填写地址。");
    var addJson = '{ "province": "0", "contactName": "' + concatName + '", "city": "' + cityId + '", "region": "' + areaId + '", "street": "' + streetId + '", "address": "' + addressStr + '", "tel": "' + concatTel + '" }';
    console.log(addJson);
    wx.request({/**通过code获取openid**/
      url:   getApp().globalData.server_support + "/collect-scrap/saveUserAddr",
      data: addJson,
      header: {
        'token': getApp().globalData.token,
        'content-type': 'application/json;charset=utf-8'
      },
      method: 'POST',
      success: function (res) {
        console.log(res);
        if (res.data.success) {
          wx.switchTab({
            url: '../../pages/order/order'
          })
        } else {
          console.log("未填写过地址");
        }
      }
    })
    console.log("结束填写地址。");
  },
  onShow: function () {
    var that = this;
    console.log("tarBar点击")
    if (app.globalData.addressStatus) {
      wx.switchTab({
        url: '../../pages/order/order'
      })
    }
    wx.setNavigationBarTitle({
      title: '三轮兄弟'
    })
    this.setData({
      userType: app.globalData.userType
    })
  }
});

module.exports = {
  citys,
  areas,
  streets
}